package com.yunhe.inspect.service.impl;

import com.yunhe.common.constant.InSpectConsts;
import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.util.DateTimeUtils;
import com.yunhe.inspect.domain.DataInspectOrder;
import com.yunhe.inspect.domain.InspectProcess;
import com.yunhe.inspect.domain.RectificationSheet;
import com.yunhe.inspect.repository.DataInspectOrderRepository;
import com.yunhe.inspect.repository.InspectProcessRepository;
import com.yunhe.inspect.repository.RectificationSheetRepository;
import com.yunhe.inspect.service.RectificationSheetService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @author zhengbo
 * @date 2021/11/16 10:54
 */
@Service
public class RectificationSheetServiceImpl implements RectificationSheetService {

    @Autowired
    private RectificationSheetRepository rectificationSheetRepository;

    @Autowired
    private DataInspectOrderRepository dataInspectOrderRepository;

    @Autowired
    private InspectProcessRepository inspectProcessRepository;

    @Override
    public void updateRectificationSheetByPrimaryKeySelective(RectificationSheet rectificationSheet) throws ArgumentErrorException {
        if (rectificationSheet.getId() == null) {
            throw new ArgumentErrorException("整改单id不能为空");
        }
        if (rectificationSheet.getInspectOrderId() == null) {
            throw new ArgumentErrorException("巡检工单id不能为空");
        }
        rectificationSheetRepository.updateRectificationSheetByPrimaryKeySelective(rectificationSheet);
        //提交整改单的时候需要设置内容已更新
        InspectProcess inspectProcess = inspectProcessRepository.getReviewProcessByOrderId(rectificationSheet.getInspectOrderId());
        if (inspectProcess != null) {
            inspectProcess.setContentUpdateFlag(true);
            inspectProcessRepository.updateInspectProcessByPrimaryKeySelective(inspectProcess);
        }

    }

    @Override
    public void createRectificationSheet(RectificationSheet rectificationSheet) {
        rectificationSheetRepository.updateRectificationSheetByPrimaryKeySelective(rectificationSheet);
    }

    @Override
    public void setRectificationSheetDeadlineStatus() {
        //2天到毫秒
        long twoDay = 2*24*60*60*1000;
        //只查询未完成的巡检单
        List<DataInspectOrder> unFinishInspectOrder = dataInspectOrderRepository.getUnFinishInspectOrder();
        if (CollectionUtils.isNotEmpty(unFinishInspectOrder)) {
            for (DataInspectOrder dataInspectOrder : unFinishInspectOrder) {
                //通过巡检单查询整改单
                List<RectificationSheet> rectificationSheetList = rectificationSheetRepository.searchRectificationSheetByInspectOrderId(Long.valueOf(dataInspectOrder.getId()));
                if (CollectionUtils.isNotEmpty(rectificationSheetList)) {
                    for (RectificationSheet rectificationSheet : rectificationSheetList) {
                        long timeDifference = DateTimeUtils.compareDateStr(DateTimeUtils.todayStr("yyyy-MM-dd HH:mm:ss"), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(rectificationSheet.getRectificationDeadline()),"yyyy-MM-dd HH:mm:ss");
                        //判断整改时间是否临近，当前时间与整改时间小于等于2天为即将到期
                        if (timeDifference > twoDay) {
                            //正常
                            rectificationSheet.setDeadlineStatus(InSpectConsts.Rectification_Status.Normal.value());
                        } else if (timeDifference >= 0) {
                            //即将到期
                            rectificationSheet.setDeadlineStatus(InSpectConsts.Rectification_Status.Expiring.value());
                        } else {
                            //超时
                            rectificationSheet.setDeadlineStatus(InSpectConsts.Rectification_Status.TimedOut.value());
                        }
                        rectificationSheetRepository.updateRectificationSheetByPrimaryKeySelective(rectificationSheet);
                    }
                }
            }
        }
    }
}
